package com.csyf.manager.controller.agent;

import com.csyf.manager.entity.SysAgent;
import com.csyf.manager.entity.SysMerchant;
import com.csyf.manager.entity.SysTrade;
import com.csyf.manager.entity.SysUser;
import com.csyf.manager.service.OrderService;
import com.csyf.manager.service.SysAgentService;
import com.csyf.manager.service.SysMerchBiz;
import com.csyf.manager.utils.page.JqGridVo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @Author: casper
 * @Description: 代理商订单Controller
 * @Date: Created in 2018/11/30 12:20
 */
@Controller
@RequestMapping("/agentOrder")
public class AgentOrderController {


    @Autowired
    private OrderService orderService;

    @Autowired
    private SysAgentService sysAgentService;

    @Autowired
    private SysMerchBiz sysMerchBiz;


    /**
     * 代理商订单列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @PostMapping(value = "list")
    @ResponseBody
    public JqGridVo list(SysTrade vo, Integer page, Integer rows) {

        //获取登录的代理商信息
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        SysAgent agent = sysAgentService.queryAgentByUserId(sysUser.getUserId());

        //获取代理商下的商户信息
        Set<Integer> merchIds = sysMerchBiz.findMerchByAgentId(agent.getId());
        List<SysMerchant> merchants = new ArrayList<>();
        for (Integer mchId : merchIds) {
            SysMerchant sysMerchant = sysMerchBiz.queryAgentById(Long.valueOf(mchId));
            merchants.add(sysMerchant);
        }

        return orderService.queryOrderByUserId(vo, page, rows);
    }

}
